import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { ApplyManageTab, getManageTabSwitch } from '../mconfig';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang';

/**
 * 获取仓库树列表
 * @param getStorageCallback
 * @returns
 */
const getStorageTreeService = (getStorageCallback: (res: any) => void, curRes?: any) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = curRes && getManageTabSwitch(curRes);

  const getStorage = useRequest(
    () => {
      let personUrl = '',
        requsetBody = {};
      if (curRes) {
        personUrl = `${baseUrl}/assetStore/treeList/${queryTypeObj.params}`;
        requsetBody = { ...queryTypeObj, storeState: 1 };
      } else {
        personUrl = `${baseUrl}/assetStore/treeList/createCompanyID`;
        requsetBody = { createCompanyID: getCompanyID(), storeState: 1 };
      }
      return requestFunComponFiex('POST', personUrl, requsetBody);
    },
    {
      manual: true,
      onSuccess: (res) => {
        getStorageCallback(res);
      },
    },
  );
  return {
    getStorageRun: getStorage?.run,
    getStorageLoading: getStorage?.loading,
  };
};

/**
 * 获取调剂资产列表
 * @param getAssetListCallback
 * @returns
 */
const getAssetList = (getAssetListCallback: (res: any) => void, curRes?: any) => {
  /**获取权限参数 */
  // const queryTypeObj: Record<string, any> = curRes && getManageTabSwitch(curRes);

  const getAsset = useRequest(
    (id: any) => {
      let personUrl = `${baseUrl}/assetMaterial/getAssetList/${id}`;
      let requsetBody = {};
      // if (curRes) {
      //   personUrl = `${baseUrl}/assetMaterial/getAssetList/${id}`;
      //   requsetBody = { ...queryTypeObj, storeState: 1 };
      // } else {
      //   personUrl = `${baseUrl}/assetStore/treeList/createCompanyID`;
      //   requsetBody = { createCompanyID: getCompanyID(), storeState: 1 };
      // }
      return requestFunComponFiex('GET', personUrl, requsetBody);
    },
    {
      manual: true,
      onSuccess: (res) => {
        getAssetListCallback(res);
      },
    },
  );
  return {
    getAssetListRun: getAsset?.run,
    getAssetListLoading: getAsset?.loading,
  };
};

/**
 * 获取验收入库详情
 * @param getDetailCallback
 * @returns
 */
const getDetailService = (getDetailCallback: (res: any) => void) => {
  const getDetail = useRequest(
    (id: any) => {
      let personUrl = `${baseUrl}/assetApplyTransfer/detail/${id}/${getUserID()}`;
      const body = {
        id: id,
      };

      return requestFunComponFiex('GET', personUrl, body);
    },
    {
      manual: true,
      onSuccess: (res) => {
        getDetailCallback(res);
      },
    },
  );
  return {
    getDetailRun: getDetail?.run,
    getDetailLoading: getDetail?.loading,
  };
};

/**
 * 添加入库提交流程
 * @param addStorageCallback
 * @returns
 */
const addStorageService = (addStorageCallback: (res: any) => void) => {
  const addStorage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyTransfer/add`;
      let bodyParams = {
        ...params,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        state: `${getTenantID()}${ILoadMapTypeAll.STORAGE_TRANSFER_STATUS}1000`, // 起草状态
        businessType: `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1015`, // 审批流需要用到的
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        addStorageCallback(res);
      },
    },
  );

  return {
    addStorageRun: addStorage?.run,
    addStorageLoading: addStorage?.loading,
  };
};

/**
 * 编辑入库提交流程
 * @param editStorageCallback
 * @returns
 */
const editStorageService = (editStorageCallback: (res: any) => void) => {
  const editStorage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyTransfer/edit`;
      let bodyParams = {
        ...params,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
        businessType: `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1015`, // 审批流需要用到的
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        editStorageCallback(res);
      },
    },
  );

  return {
    editStorageRun: editStorage?.run,
    editStorageLoading: editStorage?.loading,
  };
};

/** 资产调拨审批通过后调用 */
const approvedAssetAllotSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyTransfer/approved`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    approvedAssetAllotUseRun: request?.run,
    approvedAssetAllotUseLoading: request?.loading,
  };
};

/** 校验锁定回退资产和物料数量 */
const dealNumAssetAllotSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyTransfer/dealNum/${payload.dealType}`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    dealNumAssetAllotRun: request?.run,
    dealNumAssetAllotLoading: request?.loading,
  };
};

/**
 * 删除申请
 * @param callback 接口回调
 * @returns
 */
const deleteApplyManageService = (callback: (res: any, clickType: boolean) => void) => {
  const deleteApplyManage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyTransfer/cancel`;
      let bodyParams = {
        ...params,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    deleteApplyManageRun: deleteApplyManage?.run,
    deleteApplyManageLoading: deleteApplyManage?.loading,
  };
};

export {
  addStorageService,
  editStorageService,
  deleteApplyManageService,
  getStorageTreeService,
  getDetailService,
  approvedAssetAllotSerivice,
  dealNumAssetAllotSerivice,
  getAssetList,
};
